package com.love.driver.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.love.common.security.utils.SecurityUtils;
import com.love.driver.enums.DriverInfoTypeEnum;
import com.love.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.love.common.log.annotation.Log;
import com.love.common.log.enums.BusinessType;
import com.love.common.security.annotation.RequiresPermissions;
import com.love.driver.domain.LoveDriverInfo;
import com.love.driver.service.ILoveDriverInfoService;
import com.love.common.core.web.controller.BaseController;
import com.love.common.core.web.domain.AjaxResult;
import com.love.common.core.utils.poi.ExcelUtil;
import com.love.common.core.web.page.TableDataInfo;

/**
 * 代驾司机信息档案Controller
 * 
 * @author love
 * @date 2025-08-27
 */
@RestController
@RequestMapping("/info")
public class LoveDriverInfoController extends BaseController
{
    @Autowired
    private ILoveDriverInfoService loveDriverInfoService;

    /**
     * 查询代驾司机信息档案列表
     */
    @RequiresPermissions("driver:info:list")
    @GetMapping("/list")
    public TableDataInfo list(LoveDriverInfo loveDriverInfo)
    {
        startPage();
        List<LoveDriverInfo> list = loveDriverInfoService.selectLoveDriverInfoList(loveDriverInfo);
        return getDataTable(list);
    }

    /**
     * 导出代驾司机信息档案列表
     */
    @RequiresPermissions("driver:info:export")
    @Log(title = "代驾司机信息档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoveDriverInfo loveDriverInfo)
    {
        List<LoveDriverInfo> list = loveDriverInfoService.selectLoveDriverInfoList(loveDriverInfo);
        ExcelUtil<LoveDriverInfo> util = new ExcelUtil<LoveDriverInfo>(LoveDriverInfo.class);
        util.exportExcel(response, list, "代驾司机信息档案数据");
    }

    /**
     * 获取代驾司机信息档案详细信息
     */
    @RequiresPermissions("driver:info:query")
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable("driverId") Long driverId)
    {
        return success(loveDriverInfoService.selectLoveDriverInfoByDriverId(driverId));
    }

    /**
     * 新增代驾司机信息档案
     */
    @RequiresPermissions("driver:info:add")
    @Log(title = "代驾司机信息档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoveDriverInfo loveDriverInfo)
    {
        return toAjax(loveDriverInfoService.insertLoveDriverInfo(loveDriverInfo));
    }

    /**
     * 修改代驾司机信息档案
     */
    @RequiresPermissions("driver:info:edit")
    @Log(title = "代驾司机信息档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoveDriverInfo loveDriverInfo)
    {
        return toAjax(loveDriverInfoService.updateLoveDriverInfo(loveDriverInfo));
    }

    /**
     * 修改代驾司机信息档案
     */
    @RequiresPermissions("driver:info:status")
    @Log(title = "代驾司机信息档案", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult status(@RequestBody LoveDriverInfo loveDriverInfo)
    {
        return toAjax(loveDriverInfoService.updateLoveDriverInfo(loveDriverInfo));
    }

    /**
     * 删除代驾司机信息档案
     */
    @RequiresPermissions("driver:info:remove")
    @Log(title = "代驾司机信息档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{driverIds}")
    public AjaxResult remove(@PathVariable Integer[] driverIds)
    {
        return toAjax(loveDriverInfoService.deleteLoveDriverInfoByDriverIds(driverIds));
    }

    /**
     * 判断司机是否已注册
     */
    @GetMapping("/isDriver")
    public AjaxResult isDriver()
    {
        // 从上下文获取当前登录用户（由 Token 解析后自动注入）
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Long userId = currentUser.getUserid(); // 获取用户ID
        LoveDriverInfo loveDriverInfo = loveDriverInfoService.selectLoveDriverInfoByDriverId(userId);
        if (loveDriverInfo != null && "2".equals(loveDriverInfo.getStatus())) {
            return AjaxResult.success("已注册",loveDriverInfo);
        }
        return AjaxResult.success("未注册");
    }

    /**
     * 获取所有驾照类型
     * @return 包含驾照类型的 AjaxResult
     */
    @GetMapping("/getDriverLicenseTypes")
    public AjaxResult getDriverLicenseTypes() {
        // 调用枚举类的 getAllTypes 方法获取所有驾照类型列表
        List<String> licenseTypes = DriverInfoTypeEnum.getAllValues();
        // 将数据封装到 AjaxResult 中返回给前端
        return AjaxResult.success(licenseTypes);
    }

    /**
     * 新增代驾司机信息档案
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody LoveDriverInfo loveDriverInfo)
    {
        return toAjax(loveDriverInfoService.registerDriverInfo(loveDriverInfo));
    }
}
